package com.example.test8326;

import android.os.Bundle;
import android.util.Log;
import android.app.Activity;

import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Sharedpreferences extends Activity {
    EditText name,age,height;
    Button btn1,btn2,btn3;
    private static final String TAG = "SharedPreferencesTest";
    /**
     * 保存数据SharedPreferences文件的名字
     */
    private final String PREFS_NAME = "MyPrefsFile";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);
        name = (EditText) findViewById(R.id.name);
        age = (EditText) findViewById(R.id.age);
        height = (EditText) findViewById(R.id.height);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn1.setOnClickListener(new ButtonListener());
        btn2.setOnClickListener(new ButtonListener());
        btn3.setOnClickListener(new ButtonListener());
    }
    private class  ButtonListener implements View.OnClickListener{
        public void onClick(View v){
            switch(v.getId()){
                case R.id.btn1:
                    getUserInfo();
                    break;
                case R.id.btn2:
                    saveUserInfo();
                    break;
                case R.id.btn3:
                    clearUserInfo();
                    break;
            }
        }
    }
    /**
     * 保存用户信息
     */
    private void saveUserInfo(){
        android.content.SharedPreferences userInfo = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        android.content.SharedPreferences.OnSharedPreferenceChangeListener changeListener = new android.content.SharedPreferences.OnSharedPreferenceChangeListener() {

            @Override
            public void onSharedPreferenceChanged(android.content.SharedPreferences preferences, String key) {
                //preferences被 编辑的SharedPreferences实例
                //该SharedPreferences中被编辑的条目所对应的key
            }
        };
        //userInfo注册监听事件
        userInfo.registerOnSharedPreferenceChangeListener(changeListener);
        android.content.SharedPreferences.Editor editor = userInfo.edit();//获取Editor
        //得到Editor后，写入需要保存的数据
        editor.putString("name",name.getText().toString());
        editor.putString("age", age.getText().toString());
        editor.putString("height", height.getText().toString());
        editor.commit();//提交修改
        name.setText("");
        age.setText("");
        height.setText("");
        Log.i(TAG, "保存用户信息成功");
    }

    /**
     * 读取用户信息
     */
    private void getUserInfo(){
        android.content.SharedPreferences userInfo = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        String username = userInfo.getString("name", null);//读取username
        String userage = userInfo.getString("age", null);//读取age
        String userheight = userInfo.getString("height",null);//读取身高
        Log.i(TAG, "读取用户信息");
        Log.i(TAG, "name:" + username + "， age:" + age + "，height" + height);

        name.setText(username);
        age.setText(userage);
        height.setText(userheight);
    }


    /**
     * 清空数据
     */
    private void clearUserInfo(){
        SharedPreferences userInfo = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = userInfo.edit();//获取Editor
        editor.clear();
        editor.commit();
        name.setText("");
        age.setText("");
        height.setText("");
        Log.i(TAG, "清空数据");
    }

}